<?php

class preference extends baseModel {

    private static $instance;
    private $_prefrence;

    function __construct() {
        parent::__construct();
        $this->getAll();
    }

    public static function newInstance() {
        if (!self::$instance instanceof self) {
            self::$instance = new self ();
        }
        return self::$instance;
    }

    function getTableName() {
        return 'preference';
    }

    function getPreference($key, $section='site') {

        if ($key == '') {
            return false;
        }



        if (isset($this->_prefrence[$section][$key])) {
            return $this->_prefrence[$section][$key];
        } else {
            return FALSE;
        }
    }
    
    function setPreference($key,$value,$section='site'){
        
        
        $fields=array();
        $fields['value']=$value;
        
        $args=array();
        $args['name']=$key;
        $args['section']=$section;
        $this->update($args,$fields);
        
    }
    
    function save(){
        
    }

    function getAll() {
        $sql = sprintf('SELECT * FROM %s', $this->getTableName());
        $args = array();
        $result = $this->query($sql, $args);
        if (!is_array($this->_prefrence)) {
            $this->_prefrence = array();
        }

        foreach ($result as $key => $value) {

            $this->_prefrence[$value['section']][$value['name']] = $value['value'];
        }
    }

    function display() {
        print_r($this->_prefrence);
    }

}

?>